﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Century.Common.Business;
using Century.Common.Business.BOL.Common;
using Telerik.Web.Mvc;
using Century.Models.Common;
using System.Collections;
using Century.Web.Security.Attributes;
using Century.Web.UI.MVC;
using Century.Business.Repository;

namespace Century.Admin.Controllers
{
    //[Kooboo.CMS.Web.Authorizations.Authorization(AreaName = "AdminTools", Group = "", Name = "GenericNews")]
    [Authorization(Group = "", Name = "GenericNews")]
    public class GenericNewsController : CenturyAdminBaseController
    {
        
        // GET: /AdminTools/CountrySiteRss/
        GenericNewsBSO db = new GenericNewsBSO();
        GenericNewsTypeBSO dbtbso = new GenericNewsTypeBSO();
        CountrySiteBSO countryDb = new CountrySiteBSO();
        
        public ActionResult Index()
        {
            GetCommonForeignKeys();
            return View();
        }

        [GridAction]
        public ActionResult GetCountryRssList()
        {
            var model = new GridModel
            {
                Data = db.GetAll(c => c.IsExternal == false && c.CountrySiteId == PersistData.CurrentSite.Id).OrderByDescending(c => c.Id)
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditCountryRss(int id)
        {
            string ownedSitePathes = PersistData.CurrentSite.Path;
            GenericNews countrySiteRss = db.Find(p => p.Id == id);
            TryUpdateModel(countrySiteRss);
            countrySiteRss.IsActive = countrySiteRss.IsActive == null ? countrySiteRss.IsActive = true : countrySiteRss.IsActive;
            countrySiteRss.UnPublishDate = null;
            countrySiteRss.Source = "Century";
            countrySiteRss.IsExternal = false;
            countrySiteRss.CreatedDate = DateTime.Now;
            db.Update(countrySiteRss);

            var d = new GridModel
            {
                Data = db.GetAll(c => c.IsExternal == false && c.CountrySiteId == PersistData.CurrentSite.Id).OrderByDescending(c => c.Id)
                //Data = db.GetAll(c => c.IsExternal == false && (ownedSitePathes.Contains(c.CountrySite.Path.ToLower()) || ownedSitePathes == "*")).OrderByDescending(c => c.Id)
            };
            return View(d);


        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertCountryRss(GenericNews mdl)
        {

            string ownedSitePathes = PersistData.CurrentSite.Path;
            if (ModelState.IsValid)
            {
                mdl.IsActive = mdl.IsActive == null ? mdl.IsActive = true : mdl.IsActive;
                mdl.UnPublishDate = null;
                mdl.Source = "Century";
                mdl.IsExternal = false;
                mdl.CreatedDate = DateTime.Now;
                //The model is valid - insert the product.
                db.Insert(mdl);

                //Rebind the grid    
            }

            var d = new GridModel
            {
                Data = db.GetAll(c => c.IsExternal == false && c.CountrySiteId == PersistData.CurrentSite.Id).OrderByDescending(c => c.Id)
                //Data = db.GetAll(c => c.IsExternal == false && (ownedSitePathes.Contains(c.CountrySite.Path.ToLower()) || ownedSitePathes == "*")).OrderByDescending(c => c.Id)
            };


            return View(d);
        }

        [GridAction]
        public ActionResult GetWebinarList()
        {
            ViewBag.GenericNewsType = dbtbso.GetList().Where(c => c.Id == 1 || c.Id == 2).Select(c => new { c.Id, c.Name }).AsEnumerable();
            ViewBag.CountrySitePath = Token.AccessibleSites.Select(c => new { c.Id, c.Path }).OrderBy(c => c.Path).AsEnumerable(); //dbcs.GetList(a => a.IsActive == true && a.IsDefault == true && a.Countrysi ).Select(c => new { c.Id, c.Path }).OrderBy(c => c.Path).AsEnumerable();
            var model = new GridModel
            {

                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id && c.GenericNewsTypeId == 4).OrderBy(c => c.Id)
            };

            return View(model);

        }

        [GridAction]
        [AcceptVerbs(HttpVerbs.Post),ValidateInput(false)]
        public ActionResult UpdateWebinar(int id)
        {
            //Webinar updateWebinar = new Webinar();

            GenericNews updateWebinar = new GenericNews();

            updateWebinar = db.Find(c => c.Id == id);
            TryUpdateModel(updateWebinar);
            updateWebinar.GenericNewsTypeId = 4;
            updateWebinar.CreatedDate = DateTime.Now;
            updateWebinar.IsActive = true;
            updateWebinar.Source = "Century";


            db.Update(updateWebinar);

            var model = new GridModel
            {
                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id && c.GenericNewsTypeId == 4).OrderBy(c => c.Id)
            };
            return View(model);

        }

        [AcceptVerbs(HttpVerbs.Post),ValidateInput(false)]
        [GridAction]
        public ActionResult InsertWebinar(GenericNews webinar)
        {
            webinar.GenericNewsTypeId = 4;
            webinar.CreatedDate = DateTime.Now;
            webinar.IsActive = true;
            webinar.Source = "Century";
            webinar.Title = "Webinar";
            if (ModelState.IsValid)
            {
                db.Insert(webinar);
            }

            var model = new GridModel
            {

                Data = db.GetAll(c => c.CountrySiteId == PersistData.CurrentSite.Id && c.GenericNewsTypeId == 4).OrderBy(c => c.Id)
            };

            GetCommonForeignKeys();
            return View(model);

        }

        [GridAction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteWebinar(int id)
        {

            GenericNews deleteWebinar = new GenericNews();
            deleteWebinar = db.Find(c => c.Id == id);
            db.Delete(deleteWebinar);


            var model = new GridModel
            {

                Data=db.GetAll(c=> c.CountrySiteId==PersistData.CurrentSite.Id && c.GenericNewsTypeId==4).OrderBy(c=>c.Id)
            };
            GetCommonForeignKeys();

            return View(model);    
        }

        public void GetCommonForeignKeys()
        {
            ViewBag.GenericNewsType = dbtbso.GetList().Where(c => c.Id == 1 || c.Id == 2).Select(c => new { c.Id, c.Name }).AsEnumerable();
            ViewBag.CountrySitePath = Token.AccessibleSites.Select(c => new { c.Id, c.Path }).OrderBy(c => c.Path).AsEnumerable(); 
        }
    }
}

